<template>
  <div>
    <h3>内容页</h3>
    <img :src="itemObj.sku_img_url" class="itemimg" />
    <div>{{ itemObj.sku_name }}</div>
    <div class="btn" @click="addCart">添加购物车</div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "Item",
  data() {
    return {
      itemObj: {}, //接收某个商品的数据
      item_id: 0, //保存  item_id
      item_price: 0, //保存 商品价格
    };
  },
  created() {
    console.log(this.$route.params);
    //解构
    var { id, price } = this.$route.params;
    this.item_id = id; //保存  item_id
    this.item_price = price;
    axios.get("/json/item_" + this.item_id + ".json").then((res) => {
      console.log(res);
      this.itemObj = res.data.data.reviews[0];
    });
  },
  methods: {
    //添加购物车的函数
    addCart: function () {
      var item = {
        item_id: this.item_id,
        sku_name: this.itemObj.sku_name,
        sku_img_url: this.itemObj.sku_img_url,
        item_price: this.item_price,
        num: 1,
      };
      console.log(item);
      //调用  mutations   ,使用 commit  将state提交给 mutation
      //this.$store.commit("ADD_CART", item);

      //使用 dispatch  将 状态值  派发给 actions
      this.$store.dispatch("ADD_CART_AC", item);
    },
  },
  components: {},
};
</script>
<style scoped>
.itemimg {
  width: 100%;
}
.btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: lightgreen;
  color: lightsalmon;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bold;
}
</style>